<?php 
if (!defined("LEVEL") || LEVEL < 20 || !defined("OK")) { header('location: http://'.$_SERVER["HTTP_HOST"].''); exit; }
if (isset($url['p']) && isnum($url['p']) && $url['p'] > 0) { $p = escape(ceil((int)$url['p'])); } else { $p = 0; }
$limit = 50;
$viso = kiek("private_msg");

//Nustatom pagal ka rusiuosim
if (isset($url['o']) && !empty($url['o'])) {
	switch ($url['o']) {
		case "Kas": {
			$order = "`from`";
			break;
		}
		case "PM": {
			$order = "`read`";
			break;
		}
		case "Kam": {
			$order = "`to`";
			break;
		}
		case "Pavadinimas": {
			$order = "`title`";
			break;
		}
		case "Kada": {
			$order = "`date`";
			break;
		}
		default: {
			$order = "`id`";
			break;
		}
	}
}
//nustatom mazejancia ar didejancia tvarka
if (isset($url['w']) && !empty($url['w'])) {
	switch ($url['w']) {
		case "d": {
			$order .= " DESC";
			break;
		}
		case "a": {
			$order .= " ASC";
			break;
		}
		default: {
			$order .= " ASC";
			break;
		}
	}
}
else { $order = "`id` DESC"; }

// Trinam laiska
if (isset($url['d']) && isnum($url['d'])) {
	if ($url['d'] == "0" && isset($_POST['to']) && !empty($_POST['to']) && $_POST['del_all'] == 'Valyti') {
			$sql = mysql_query1("DELETE FROM `".LENTELES_PRIESAGA."private_msg` WHERE `to`=".escape($_POST['to'])."") or die(mysql_error());
			$i = mysql_affected_rows();
			if ($i > 0) { msg("Išvalyta","Ištryniau visus <b>".input($_POST['to'])."</b> laiškus.<br/> Viso ".$i.""); redirect("?id,46;a,20","meta"); }
			else { klaida("Klaida","Nepavyko ištrinti"); }
	}
	if ($url['d'] == "0" && isset($_POST['from']) && !empty($_POST['from']) && $_POST['del_all'] == 'Valyti') {
			$sql = mysql_query1("DELETE FROM `".LENTELES_PRIESAGA."private_msg` WHERE `from`=".escape($_POST['from'])."") or die(mysql_error());
			$i = mysql_affected_rows();
			if ($i > 0) { msg("Išvalyta","Ištryniau visus <b>".input($_POST['to'])."</b> laiškus.<br/> Viso ".$i.""); redirect("?id,46;a,20","meta"); }
			else { klaida("Klaida","Nepavyko ištrinti"); }
	}
	elseif (!empty($url['d']) && $url['d'] > 0) {
		mysql_query("DELETE FROM `".LENTELES_PRIESAGA."private_msg` WHERE id=".escape((int)$url['d'])) or die(mysql_error());
		header("Location: ".url('d,0'));
	}
}


//perziureti laiska
if (isset($url['v'])) {
	if(!empty($url['v']) && (int)$url['v'] > 0) {
		$sql = mysql_fetch_assoc(mysql_query1("SELECT `msg`, `from`, `title` FROM `".LENTELES_PRIESAGA."private_msg` WHERE `id`=".escape((int)$url['v'])." LIMIT 1"));
		if (mysql_affected_rows() > 0) {
			$text = "
				<fieldset>
				<legend>Privačios žinutės tekstas:</legend>
				<table border='0' width='100%'>
					<tr>
						<td class=\"title\" align=\"left\">Nuo: " . $sql['from'] ." Tema: ".input($sql['title'])."</td>
					</tr>
					<tr>
						<td width=\"50%\"  class=\"sarasas\" style=\"background-color: #FFFFCC\"><div align=\"justify\"><p>" . bbcode($sql['msg']) ."</p></div></td>
					</tr>
				</table>
				<form name=\"replay_pm\" action='?id,45;n,1;u," . $sql['from'] .";i," . $url['v'] ."' method=\"post\">
					<input type=\"submit\" value=\"Atsakyti\"/> <input type=\"button\" value=\"Trinti\" onclick=\"location.href='".url("d,".$url['v'].";v,0")."'\"/>
				</form>
				</fieldset>
			";
			lentele("Laiškas",$text);
		}
		else { klaida("Klaida","Toks laiškas neegzistuoja"); }
	}
}


//paruosiam klase lenteliu paisymui
include_once("priedai/class.php");

		//laisku saras
		unset($info);
		$sql = mysql_query1("
			SELECT SUBSTRING(`msg`,1,50) AS `msg`,
			(SELECT `id` AS `nick_id` FROM `".LENTELES_PRIESAGA."users` WHERE `nick`= `".LENTELES_PRIESAGA."private_msg`.`from`) AS `from_id`,
			(SELECT `id` AS `nick_id` FROM `".LENTELES_PRIESAGA."users` WHERE `nick`= `".LENTELES_PRIESAGA."private_msg`.`to`) AS `to_id`,
			`".LENTELES_PRIESAGA."private_msg`.`id`, `".LENTELES_PRIESAGA."private_msg`.`from` AS `from_nick`, `".LENTELES_PRIESAGA."private_msg`.`to` AS `to_nick`, `".LENTELES_PRIESAGA."private_msg`.`title`, `".LENTELES_PRIESAGA."private_msg`.`read`, `".LENTELES_PRIESAGA."private_msg`.`date`
			FROM `".LENTELES_PRIESAGA."private_msg` ORDER BY $order LIMIT ".escape($p).",".$limit);
		while($row = mysql_fetch_assoc($sql)) {
			if ($row['read'] == "NO") {
				$extra ="<img src='images/pm/pm_new.png' />";
			} else {
				$extra = "<img src='images/pm/pm_read.png' />";
			}
			$info[] = array(
			"PM"=>$extra,
			"Kas"=> "<a href=\"?id,47;m,".(!empty($row['from_id'])?$row['from_id']."\"":"0\" style='text-decoration: line-through'")." title=\"".$row['from_nick']."\">".$row['from_nick']."</a> <a href=\"?id,45;n,1;u,".$row['from_nick']."\"><img src=\"images/pm/mail.png\" alt=\"pm\" border=\"0\"></img></a>",
			"Kam"=> "<a href=\"?id,47;m,".(!empty($row['to_id'])?$row['to_id']."\"":"0\" style='text-decoration: line-through'")." title=\"".$row['to_nick']."\">".$row['to_nick']."</a> <a href=\"?id,45;n,1;u,".$row['to_nick']."\"><img src=\"images/pm/mail.png\" alt=\"pm\" border=\"0\"></img></a>",
			"Pavadinimas"=>"<a href=\"?id,46;a,20;v,".$row['id']."\" title=\"header=[Laiško ištrauka:] body=[".input(trim(strip_tags(str_replace(array('[',']'),'',$row['msg']))))."...] fade=[on]\" style=\"display:block\">".trimlink(input($row['title']),60)."</a>",
			//"Pavadinimas"=>"<a href=\"?id,46;a,20;v,".$row['id']."\" title=\"header=[Laiško ištrauka:] body=[".input(bbcode($sql['msg']))."...] fade=[on]\">".input($row['title'])."</a>",
			"Kada" => kada($row['date']),
			"Valdymas"=>"<button onclick=\"if (confirm('Ar tikrai nori ištrinti?')) window.location='".url("d,".$row['id']."")."'; else return false;\">X</button>"
			);
		}
		//nupiesiam laisku lentele
		$bla = new Table();
		if ($viso > $limit) { lentele("Puslapiai:",puslapiai($p,$limit,$viso,10)); }
		lentele("Laiškai",$bla->render($info),false,array('Viso',$viso));
		if ($viso > $limit) { lentele("Puslapiai:",puslapiai($p,$limit,$viso,10)); }
		unset($info,$row,$viso,$limit,$p);
		
		//laisku trinimas "kam siustu laisku"
		$sql = mysql_query1("SELECT count(*) AS 'viso', `to` AS 'nick' FROM `".LENTELES_PRIESAGA."private_msg` GROUP BY `to` ORDER BY `to`");
		if (mysql_num_rows($sql) > 0) {
			while($row = mysql_fetch_assoc($sql)) {
				$select[$row['nick']] = $row['nick']." - ".$row['viso'];
			}
			$nustatymai = array(
				"Form"=>array("action"=>url('d,0'),"method"=>"post","enctype"=>"","id"=>"","class"=>"","name"=>"reg"),
				"Trinti pagal NICK:"=>array("type"=>"select","value"=>$select,"selected"=>$_SESSION['username'],"name"=>"to"),
				""=>array("type"=>"submit","name"=>"del_all","value"=>"Valyti")
				);
			$bla = new forma();
			lentele("Automatinis laiškų trinimas/kam",$bla->form($nustatymai));
		}
		
		//laisku tinimas "nuo ko gautu"
		$sql = mysql_query1("SELECT count(*) AS 'viso', `from` AS 'nick' FROM `".LENTELES_PRIESAGA."private_msg` GROUP BY `from` ORDER BY `from`");
		if (mysql_num_rows($sql) > 0) {
			while($row = mysql_fetch_assoc($sql)) {
				$select[$row['nick']] = $row['nick']." - ".$row['viso'];
			}
			$nustatymai = array(
				"Form"=>array("action"=>url('d,0'),"method"=>"post","enctype"=>"","id"=>"","class"=>"","name"=>"reg"),
				"Trinti pagal NICK:"=>array("type"=>"select","value"=>$select,"selected"=>$_SESSION['username'],"name"=>"from"),
				""=>array("type"=>"submit","name"=>"del_all","value"=>"Valyti")
				);
			$bla = new forma();
			lentele("Automatinis laiškų trinimas/nuo ko",$bla->form($nustatymai));
		}
unset($text);
?>
